package com.optitronics.controllers;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.optitronics.beans.EmailMessage;
import com.optitronics.service.MailSendingService;
import com.optitronics.util.OptitronicsConstants;

@RequestMapping("/emailmessageses")
@Controller
public class EmailMessagesController {

	@Autowired
	private GenericConversionService conversionService;

	@Autowired
	private MailSendingService mailSendingService;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String show(@PathVariable("id") Integer id, Model model) {
		addDateTimeFormatPatterns(model);
		model.addAttribute("emailmessages", EmailMessage.findEmailMessages(id));
		model.addAttribute("itemId", id);
		return "emailmessageses/show";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size, Model model) {
		if (page != null || size != null) {
			int sizeNo = size == null ? 10 : size.intValue();
			model.addAttribute("emailmessageses",
					EmailMessage.findEmailMessagesEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
			float nrOfPages = (float) EmailMessage.countEmailMessageses() / sizeNo;
			model.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1
					: nrOfPages));
		} else {
			model.addAttribute("emailmessageses", EmailMessage.findAllEmailMessageses());
		}
		addDateTimeFormatPatterns(model);
		return "emailmessageses/list";
	}

	@RequestMapping(value = "/{id}/delete")
	public String delete(@PathVariable("id") Integer id, @RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size, Model model) {
		EmailMessage.findEmailMessages(id).remove();
		model.addAttribute("page", (page == null) ? "1" : page.toString());
		model.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/emailmessageses";
	}

	@RequestMapping(value = "/{id}/resend")
	public String resendEmail(@PathVariable("id") Integer id, Model model) {
		mailSendingService.resendMail(id);
		return "redirect:/emailmessageses?page=1&size=" + OptitronicsConstants.PAGE_SIZE;
	}

	Converter<EmailMessage, String> getEmailMessagesConverter() {
		return new Converter<EmailMessage, String>() {
			public String convert(EmailMessage emailMessages) {
				return new StringBuilder().append(emailMessages.getFromAddress()).append(" ")
						.append(emailMessages.getSubject()).append(" ").append(emailMessages.getToAddress()).toString();
			}
		};
	}

	@PostConstruct
	void registerConverters() {
		conversionService.addConverter(getEmailMessagesConverter());
	}

	void addDateTimeFormatPatterns(Model model) {
		model.addAttribute("emailMessages_sendingdate_date_format", OptitronicsConstants.INVOICE_DATE_TIME_FORMAT);
	}

	public void setMailSendingService(MailSendingService mailSendingService) {
		this.mailSendingService = mailSendingService;
	}

}
